package com.zeng.ssm.service.config;

import org.jeecgframework.minidao.factory.MiniDaoBeanScannerConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

/**
 * 1.添加依赖
 * 2.添加JimuReportConfig
 * 3.添加扫描(mvc和service)org.jeecg.modules.jmreport
 * 4.将config目录下配置、jimu.properties、statics/jmreport放在项目中
 * 5.添加静态资源配置
 *
 * @author zengwanlin
 * @date 2022-03-11T15:23
 */
@Configuration(proxyBeanMethods = false)
public class JimuReportConfig {

    private Logger logger = LoggerFactory.getLogger(JimuReportConfig.class);

    /**
     * 扫描minidao
     *
     * @param applicationContext
     * @param miniDaoConfig
     * @return
     */
    @Bean
    public MiniDaoBeanScannerConfigurer miniDaoBeanScannerConfigurer(ApplicationContext applicationContext, JimuReportMiniDaoConfig miniDaoConfig) {
        logger.info(" ******************* init miniDao config *********************** ");
        logger.info(miniDaoConfig.toString());
        MiniDaoBeanScannerConfigurer miniDaoBeanScannerConfigurer = new MiniDaoBeanScannerConfigurer();
        miniDaoBeanScannerConfigurer.setApplicationContext(applicationContext);
        miniDaoBeanScannerConfigurer.setBasePackage(miniDaoConfig.getBasePackage());
        miniDaoBeanScannerConfigurer.setDbType(miniDaoConfig.getDbType());
        miniDaoBeanScannerConfigurer.setKeyType(miniDaoConfig.getKeyType());
        miniDaoBeanScannerConfigurer.setFormatSql(miniDaoConfig.isFormatSql());
        miniDaoBeanScannerConfigurer.setShowSql(miniDaoConfig.isShowSql());
        miniDaoBeanScannerConfigurer.setAnnotation(miniDaoConfig.getAnnotation());
        return miniDaoBeanScannerConfigurer;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(JdbcTemplate jdbcTemplate) {
        return new NamedParameterJdbcTemplate(jdbcTemplate);
    }

}
